/*************************************************************************
	> File Name: setitimer.c
	> Author: Angel Criss
	> Blog: blog.abbycin.tk 
	> Created Time: Mon 30 Jun 2014 07:44:06 PM CST
 ************************************************************************/

#include"time.h"
int _times=1;
void dealing(int sig)
{
    printf("\n\e[44m%d time's up\e[0m\n",_times);
    _times+=1;
    return;
}
int main(int argc,char *argv[])
{
    int which; //value in ITIMER_REAL/ITIMER_VIRTUAL/ITIMER_PROF
    struct itimerval new_value, old_value;
    struct sigaction act;
    sigset_t set;

    sigemptyset(&set);
    //sigaddset(&set,SIGALRM);
    act.sa_handler=dealing;
    act.sa_mask=set;
    act.sa_flags=0;

    new_value.it_interval.tv_sec=0; //it_interval 的两个值都为0则是一次性定时器
    new_value.it_interval.tv_usec=0; //若此处为1则每隔1s或1us发送一次SIGALRM信号
    new_value.it_value.tv_usec=0; //定时的us
    new_value.it_value.tv_sec=10; //定时器定时的s

    printf("this program will sleep 10s\n");

    if((setitimer(ITIMER_REAL,&new_value,NULL))==-1)
    {
        perror("-->:");
        _exit(errno);
    }
    else
    {
        if(sigaction(SIGALRM,&act,NULL)==-1)
        {
            perror("-->:");
            _exit(errno);
        }
    }
    for(;;)
    {
        if(_times>=2)
            break;
    }
}
